home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LT_LabelWidth.c < prev    next >
C/C++ Source or Header  |  1996-08-22  |  3KB  |  153 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /****** gtlayout.library/LT_LabelWidth ******************************************
  15. *
  16. *   NAME
  17. *    LT_LabelWidth -- Calculate the width of a string according
  18. *                     to the user interface font used.
  19. *
  20. *   SYNOPSIS
  21. *    Width = LT_LabelWidth(Handle,Label);
  22. *      D0                    A0    A1
  23. *
  24. *    LONG LT_LabelWidth(LayoutHandle *,STRPTR);
  25. *
  26. *   FUNCTION
  27. *    This routine calculates the width of strings in
  28. *    terms of pixels according to the user interface
  29. *    font associated with the LayoutHandle.
  30. *
  31. *    You can pass multi-line label texts to this routine,
  32. *    it will calculate the maximum length of the single
  33. *    lines. (V12)
  34. *
  35. *   INPUTS
  36. *    Handle - Pointer to LayoutHandle structure.
  37. *
  38. *   RESULT
  39. *    Width - Width of the text string in pixels.
  40. *
  41. ******************************************************************************
  42. *
  43. */
  44.  
  45. LONG LIBENT
  46. LT_LabelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR label)
  47. {
  48.     if(handle)
  49.     {
  50.         struct RastPort *rp = &handle->RPort;
  51.         LONG start;
  52.         LONG len;
  53.         LONG cnt;
  54.         LONG width,maxWidth;
  55.         LONG underscore;
  56.  
  57.         start = 0;
  58.         maxWidth = 0;
  59.         underscore = 0;
  60.  
  61.         cnt = 0;
  62.         len = 0;
  63.  
  64.         while(label[len])
  65.         {
  66.             if(label[len] == '_')
  67.             {
  68.                 if(!underscore)
  69.                     underscore = TextLength(rp,"_",1);
  70.  
  71.                 cnt += underscore;
  72.             }
  73.  
  74.             if(label[len] == '\n')
  75.             {
  76.                 if(len > start)
  77.                     width = TextLength(rp,&label[start],len - start) - cnt;
  78.                 else
  79.                     width = 0;
  80.  
  81.                 cnt = 0;
  82.                 start = len + 1;
  83.  
  84.                 if(width > maxWidth)
  85.                     maxWidth = width;
  86.             }
  87.  
  88.             len++;
  89.         }
  90.  
  91.         if(len > start)
  92.             width = TextLength(rp,&label[start],len - start) - cnt;
  93.         else
  94.             width = 0;
  95.  
  96.         if(width > maxWidth)
  97.             maxWidth = width;
  98.  
  99.         return(maxWidth);
  100.     }
  101.     else
  102.         return(0);
  103. }
  104.  
  105.  
  106. /*****************************************************************************/
  107.  
  108.  
  109. /****** gtlayout.library/LT_LabelChars ******************************************
  110. *
  111. *   NAME
  112. *    LT_LabelChars -- Calculate the width of a a string
  113. *                     according to the user interface font
  114. *                     associated with a LayoutHandle.
  115. *
  116. *   SYNOPSIS
  117. *    Length = LT_LabelChars(Handle,Label);
  118. *      D0                     A0    A1
  119. *
  120. *    LONG LT_LabelChars(struct LayoutHandle *,STRPTR);
  121. *
  122. *   FUNCTION
  123. *    Calculates the width of a string according to the user
  124. *    interface font used. The width is then converted to a number
  125. *    of characters suitable for using with the LA_Chars tag
  126. *    item when calling LT_New().
  127. *
  128. *    You can pass multi-line label texts to this routine,
  129. *    it will calculate the maximum length of the single
  130. *    lines. (V12)
  131. *
  132. *   INPUTS
  133. *    Handle - Pointer to LayoutHandle structure.
  134. *
  135. *    Label - Pointer to string.
  136. *
  137. *   RESULT
  138. *    Length - Number of characters that are considered
  139. *        equivalent to the string length in pixels.
  140. *
  141. ******************************************************************************
  142. *
  143. */
  144.  
  145. LONG LIBENT
  146. LT_LabelChars(REG(a0) LayoutHandle *handle,REG(a1) STRPTR label)
  147. {
  148.     if(handle)
  149.         return((LT_LabelWidth(handle,label) + handle->GlyphWidth - 1) / handle->GlyphWidth);
  150.     else
  151.         return(0);
  152. }
  153.